<script lang="ts" setup>
import PageDialog from "@/components/PageDialog/index.vue";
import {ref, watch} from "vue";
import {OwnerRentOrder} from "@/api/demoClass";
import {
  addOwnerRentOrder,
  ownerList,
  ownerPaymentList, projectById,
  projectList
} from "@/api/demoApi";
import {message} from "@/utils/message";

defineOptions({
  name: "ownerRentOrder"
});

/**-------------------表单相关-------------------**/
//表单参数
const formData = ref({
  id: null,
  projectId:null,
  ownerId:null,
  paymentId:null,
  orderType:null,
  orderNo: '1/1',
  orderStart: null,
  orderEnd: null,
  orderPrice: null,
  remark:null,
  status: 0
})

//表单校验
const formRules = ref({
  projectId:[{required:true,message:'请选择项目',trigger:['blur','change']}],
  ownerId:[{required:true,message:'请选择所属业主',trigger:['blur','change']}],
  paymentId:[{required:true,message:'请选择付款单位',trigger:['blur','change']}],
  orderType:[{required:true,message:'请选择订单类型',trigger:['blur','change']}],
  orderStart:[{required:true,message:'请选择开始日期',trigger:['blur','change']}],
  orderEnd:[{required:true,message:'请选择结束日期',trigger:['blur','change']}],
  orderPrice:[{required:true,message:'请输入订单金额',trigger:['blur','change']}],
})

//项目选项
const projectOptions = ref([])
const ownerOptions = ref([])
const paymentOptions = ref([])
const typeOptions = [
  {value:0,label:'租金G'},
  {value:1,label:'租金S'},
  {value:2,label:'对私年均分摊'},
  {value:3,label:'对公年均分摊'},
  {value:4,label:'保证金'},
]
const getProjectOptions = ()=>{
  projectOptions.value = projectList().map(item=>({value:item.id,label:`${item.projectNo}(${item.projectName})`}))
}
const getOwnerOptions = ()=>{
  ownerOptions.value = ownerList().map(item=>({value:item.id,label:item.ownerName}))
}
const getPaymentOptions = id =>{
  if(!id) {
    formData.value.paymentId = null
    return
  }
  paymentOptions.value = ownerPaymentList(id).map(item=>({value:item.id,label:`${item.paymentName}(${item.account})`}))
}

/**-------------------打开弹框-------------------**/
//标题
const dialogTitle = ref('')
const dialog = ref()
//弹框显示
const dialogShow = (title: string,data?: object)=>{
  dialogTitle.value = title
  getProjectOptions()
  getOwnerOptions()
  dialog.value.dialogVisible = true
}
defineExpose({
  dialogShow
})

/**-------------------事件回调-------------------**/
//选择项目
  const selectProject = val => {
    const project = projectById(val)
  if(project) {
    if(!project.ownerId) {
      message("该项目为绑定业主，请先至合同管理进行业主绑定", { type: "warning" });
      return
    }
    formData.value.ownerId = project.ownerId
    getPaymentOptions(project.ownerId)
  }
}
//取消
const form = ref()
const handlerCancel = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.resetFields()
    formData.value = {
      id: null,
      projectId:null,
      ownerId:null,
      paymentId:null,
      orderType:null,
      orderNo: '1/1',
      orderStart: null,
      orderEnd: null,
      orderPrice: null,
      remark:null,
      status: 0
    }
    dialog.value.dialogVisible = false
    resolve({})
  }catch(e){
    console.log(e)
    reject(e)
  }
})
//确认
const emits = defineEmits(['done'])
const handlerConfirm = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.validate()
    //todo 新增、编辑接口
    formData.value.remark = `${formData.value.remark}--手动创建`
    const newOwnerEle = new OwnerRentOrder(formData.value)
    addOwnerRentOrder([newOwnerEle])
    message("添加成功", { type: "success" });
    await handlerCancel()
    emits('done')
    resolve({})
  }catch(e){
    reject(e)
  }
})
</script>

<template>
  <PageDialog ref="dialog" :handler-cancel="handlerCancel" :handler-confirm="handlerConfirm" :title="dialogTitle">
    <el-form ref="form" :disabled="dialogTitle === '查看'" :model="formData" :rules="formRules" label-position="top" label-width="auto">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="formData.projectId" clearable filterable placeholder="请选择项目名称" @change="selectProject">
          <el-option v-for="(item,index) in projectOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属业主" prop="ownerId">
        <el-select v-model="formData.ownerId" clearable disabled filterable placeholder="自动回显">
          <el-option v-for="(item,index) in ownerOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="付款单位" prop="paymentId">
        <el-select v-model="formData.paymentId" :disabled="!formData.projectId" clearable filterable placeholder="请选择付款单位">
          <el-option v-for="(item,index) in paymentOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="formData.orderType" clearable filterable placeholder="请选择订单类型">
          <el-option v-for="(item,index) in typeOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="orderStart">
        <el-date-picker
          v-model="formData.orderStart"
          clearable
          format="YYYY-MM-DD"
          placeholder="请选择开始日期"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="结束日期" prop="orderEnd">
        <el-date-picker
          v-model="formData.orderEnd"
          clearable
          format="YYYY-MM-DD"
          placeholder="请选择结束日期"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="订单金额(元)" prop="orderPrice">
        <el-input-number v-model.number="formData.orderPrice" :controls="false" :precision="2" :step="0.01" placeholder="请输入订单金额(元)"/>
      </el-form-item>
      <el-form-item label="订单备注" prop="company">
        <el-input v-model="formData.remark" clearable placeholder="请输入订单备注" />
      </el-form-item>
    </el-form>
  </PageDialog>
</template>

<style lang="scss" scoped>
:deep(.el-form) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;

  .el-form-item {
    width: 48%;

    .el-input,
    .el-input-number,
    .el-date-picker,
    .el-select {
      width: 100%;
    }
  }
}
</style>
